package com.easondongh.blog.service.impl;

import com.easondongh.blog.dao.BlogCollectionDao;
import com.easondongh.blog.dao.BlogGoodsDao;
import com.easondongh.blog.mapper.BlogMapper;
import com.easondongh.blog.mapper.TypeMapper;
import com.easondongh.blog.pojo.*;
import com.easondongh.blog.service.BlogService;
import com.easondongh.blog.utils.*;
import com.easondongh.blog.vo.BlogVo;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 博客
 * @author EasonDongH
 * @date 2020/3/26 21:21
 */
@Service
public class BlogServiceImpl implements BlogService {

    private static int SUMMARY_MAX_LENGTH = 80;

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BlogGoodsDao blogGoodsDao;
    @Autowired
    private BlogCollectionDao blogCollectionDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Blog blog) {
        String dateStr = DateTimeUtils.DateToString(new Date());
        blog.setBlogId(idWorker.nextId() + "");
        blog.setCreatedTime(dateStr);
        blog.setBlogSummary(StringUtils.removeHtmlTag(blog.getBlogContent()).substring(0,SUMMARY_MAX_LENGTH));
        blogMapper.save(blog);
        // 取出分类，当前分类博客数+1
        Integer blogType = blog.getBlogType();
        Type type = typeMapper.getById(blogType);
        type.setTypeBlogCount(type.getTypeBlogCount() + 1);
        typeMapper.update(type);
    }

    @Override
    public Blog getById(String id) {
        return blogMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Blog blog) {
        String dateStr = DateTimeUtils.DateToString(new Date());
        // 修改之前先进行查询，用作下面判断是否需要修改分类统计字段
        Blog oldBlog = blogMapper.getById(blog.getBlogId());
        blog.setUpdateTime(dateStr);
        blog.setBlogSummary(StringUtils.removeHtmlTag(blog.getBlogContent().substring(0,SUMMARY_MAX_LENGTH)));
        blogMapper.update(blog);
        // 判断博客分类有没有被修改，如果被修改了，旧的分类博客数-1，新的分类博客数+1
        Integer oldTypeId = oldBlog.getBlogType();
        Integer nowTypeId = blog.getBlogType();
        if (!oldTypeId.equals(nowTypeId)) {
            Type oldType = typeMapper.getById(oldTypeId);
            oldType.setTypeBlogCount(oldType.getTypeBlogCount() - 1);
            typeMapper.update(oldType);

            Type nowType = typeMapper.getById(nowTypeId);
            nowType.setTypeBlogCount(nowType.getTypeBlogCount() + 1);
            typeMapper.update(nowType);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BlogVo readById(String id) {
        Blog blog = blogMapper.getById(id);
        // 阅读，需要更新阅读数
        blog.setBlogRead(blog.getBlogRead() + 1);
        blogMapper.update(blog);
        // 将blog转为blogVo
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(blog, blogVo);
        // 查询分类
        Type type = typeMapper.getById(blog.getBlogType());
        blogVo.setTypeName(type.getTypeName());
        return blogVo;
    }

    @Override
    public void deleteById(String id) {
        String dateStr = DateTimeUtils.DateToString(new Date());
        blogMapper.deleteById(id, dateStr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<BlogVo> getByPage(Page<BlogVo> page) {
        // 查询数据
        List<BlogVo> blogVoList = blogMapper.getByPage(page);
        page.setList(blogVoList);
        // 查询总数
        int totalCount = blogMapper.getCountByPage(page);
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public List<Blog> getRecommend() {
        return this.blogMapper.getRecommend();
    }

    @Override
    public List<Blog> getTimeline() {
        return this.blogMapper.getTimeline();
    }

    @Override
    public void goodByBlogAndUser(BlogGoods blogGoods) {
        // 更新博客
        this.blogMapper.updateGoods(blogGoods.getBlogId());
        // 更新点赞记录
        blogGoods.setId(idWorker.nextId() + "");
        this.blogGoodsDao.save(blogGoods);
    }

    @Override
    public boolean hasGoodByBlogAndUser(Integer userId, String blogId) {
        return this.blogGoodsDao.countByUserIdAndBlogId(userId, blogId) > 0;
    }

    @Override
    public void collectByBlogAndUser(BlogCollection blogCollection) {
        // 更新博客
        this.blogMapper.updateCollection(blogCollection.getBlogId());
        // 更新收藏记录
        Blog blog = this.blogMapper.getById(blogCollection.getBlogId());
        blog.setBlogContent(null);
        blogCollection.setBlog(blog);
        blogCollection.setCollectionId(idWorker.nextId() + "");
        blogCollection.setCollectionTime(DateTimeUtils.DateToString(new Date()));
        this.blogCollectionDao.save(blogCollection);
    }

    @Override
    public boolean hasCollectByBlogAndUser(Integer userId, String blogId) {
        return this.blogCollectionDao.countByUserIdAndBlogId(userId, blogId) > 0;
    }
}
